/*
 * Copyright 2012 NJUT  qixiaobo. All rights reserved.
 */
package com.fujitsu.nanjing.tieba.service;

import java.lang.reflect.InvocationTargetException;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fujitsu.nanjing.tieba.base.BaseService;
import com.fujitsu.nanjing.tieba.common.ReflectUtils;
import com.fujitsu.nanjing.tieba.constrant.CommonParamter;
import com.fujitsu.nanjing.tieba.domain.BoardInfo;
import com.fujitsu.nanjing.tieba.ibator.CoreBoard;
import com.fujitsu.nanjing.tieba.ibator.CoreBoardCriteria;

/**
 * BoardInfoService.java
 * 
 * @author Qixiaobo-win8
 */
@Transactional
@Service
public class BoardInfoService extends BaseService<BoardInfo, CoreBoardCriteria> {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(BoardInfoService.class);

	@Resource
	private TopicService topicService;
	@Resource
	private BoardService boardService;
	@Override
	public BoardInfo findById(String id) {
		return findByIdWithPageNum(id, 0);
	}

	@Override
	public BoardInfo insert(BoardInfo t) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void update(BoardInfo t) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(BoardInfo t) {
		// TODO Auto-generated method stub

	}

	public BoardInfo findByIdWithPageNum(String id, int pageNum) {
		CoreBoard board = boardService.findById(id);
		if (board == null) {
			return null;
		}
		BoardInfo info = new BoardInfo();
		try {
			ReflectUtils.copy(board, info);
		} catch (IllegalAccessException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		info.setOffset(pageNum);
		info.setLength(CommonParamter.默认分页长度);
		info.setTotalCount(topicService.countByBoardId(info.getBoardid()));
		info.setTopics(topicService.findAllByBoardIdDefault(info.getBoardid(),
				info.getOffset()));
		info.setPage((int) Math.ceil((double) topicService.countByBoardId(info
				.getBoardid()) / info.getLength()));
		return info;
	}
}
